﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using Telerik.Web.UI;
using System.ComponentModel;
using System.Data;
using MLM.Base;
using MLM.Data;

namespace MLM.AdminCP.Pages
{
    public partial class MyProfile : BasePage
    {
        //static RadComboBox combo;
        static RadListViewItemEventArgs temp;
        static String a;
        static int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CurrentUser.Value = Permission.GetCurrentUserId().ToString();
                BindData();
            }
            SaveBtn.Click += new EventHandler(SaveBtn_Click);
            ResetBtn.Click += new EventHandler(ResetBtn_Click);
        }

        void ResetBtn_Click(object sender, EventArgs e)
        {
            BindData();
        }

        void SaveBtn_Click(object sender, EventArgs e)
        {
            MLMDataContext ct = new MLMDataContext();
            long userId = Permission.GetCurrentUserId();
            var deliveryInfo = ct.UserPaymentInfos.FirstOrDefault(it => it.UserID == userId);
            var bankInfo = ct.UserBanks.FirstOrDefault(it => it.UserID == userId);
            var userInfo = ct.UserInfos.FirstOrDefault(it => it.UserID == userId);
            var user = ct.Users.FirstOrDefault(it => it.UserId == userId);
            //user & userinfo
            var myFirstName = MyFirstNameTxt.Text;
            var myLastName = MyLastNameTxt.Text;
            var myHomephone = MyHomephoneTxt.Text;
            var myMobile = MyMobileTxt.Text;
            var myAddress = MyAddressTxt.Text;
            var myEmail = MyEmailTxt.Text;
            //bank information
            var bankAccountName = BankAccountNameTxt.Text;
            var bankAccountNumber = BankAccountNumberTxt.Text;
            var bankCode = BankCodeTxt.Text;
            //var accountID = AccountIDTxt.Text;
            var bankName = BankNameTxt.Text;
            var swiftCode = SWIFTCodeTxt.Text;
            var bicCode = BICCodeTxt.Text;
            var ibanCode = IBANCodeTxt.Text;
            //
            var billAddress = BillAddressTxt.Text;
            var billFirstName = BillFirstNameTxt.Text;
            var billLastName = BillLastNameTxt.Text;
            var billpostalCode = BillPostalCodeTxt.Text;
            var billCountryId = long.Parse(BillCountryDDL.SelectedValue);
            var billCity = BillCityTxt.Text;

            user.Address = myAddress;
            user.FirstName = myFirstName;
            user.LastName = myLastName;
            if (userInfo != null)
            {
                userInfo.HomePhone = myHomephone;
                userInfo.Mobile = myMobile;
                userInfo.Email = myEmail;
            }
            else
            {
                ct.UserInfos.AddObject(new Data.UserInfo
                                           {
                                               Mobile = myMobile,
                                               HomePhone = myHomephone,
                                               Email = myEmail
                                           });
            }

            if (deliveryInfo == null)
            {
                ct.UserPaymentInfos.AddObject(new UserPaymentInfo
                                                  {
                                                      Address = billAddress,
                                                      UserID = userId,
                                                      LastName = billLastName,
                                                      FirstName = billFirstName,
                                                      CountryID = billCountryId,
                                                      CityName = billCity,
                                                      PostalCode = billpostalCode
                                                  });
            }
            else
            {
                deliveryInfo.Address = billAddress;
                deliveryInfo.UserID = userId;
                deliveryInfo.LastName = billLastName;
                deliveryInfo.FirstName = billFirstName;
                deliveryInfo.CountryID = billCountryId;
                deliveryInfo.CityName = billCity;
                deliveryInfo.PostalCode = billpostalCode;
            }
            if (bankInfo == null)
            {
                ct.UserBanks.AddObject(new Data.UserBank
                {
                    // AccountID = accountID,
                    AccountName = bankAccountName,
                    AccountNumber = bankAccountNumber,
                    BICCode = bicCode,
                    BankCode = bankCode,
                    BankName = bankName,
                    IBANCode = ibanCode,
                    SWIFTCode = swiftCode,
                    UserID = userId
                });
            }
            else
            {
                // bankInfo.AccountID = accountID;
                bankInfo.AccountName = bankAccountName;
                bankInfo.AccountNumber = bankAccountNumber;
                bankInfo.BICCode = bicCode;
                bankInfo.BankCode = bankCode;
                bankInfo.BankName = bankName;
                bankInfo.IBANCode = ibanCode;
                bankInfo.SWIFTCode = swiftCode;
                bankInfo.UserID = userId;
            }
            if (ct.SaveChanges() > 0)
            {
                Message.ShowSuccess("Save successfully!");
            }
            else
            {
                Message.ShowError("Error! Please check data again!");
            }
        }
        public void BindData()
        {
            MLMDataContext ct = new MLMDataContext();
            long userId = Permission.GetCurrentUserId();
            var deliveryInfo = ct.UserPaymentInfos.FirstOrDefault(it => it.UserID == userId);
            var bankInfo = ct.UserBanks.FirstOrDefault(it => it.UserID == userId);
            var userInfo = ct.UserInfos.FirstOrDefault(it => it.UserID == userId);
            var user = ct.Users.FirstOrDefault(it => it.UserId == userId);
            if (user != null)
            {
                MyFirstNameTxt.Text = user.FirstName ?? "";
                MyLastNameTxt.Text = user.LastName ?? "";
                MyAddressTxt.Text = user.Address ?? "";                
            }
            if (userInfo != null)
            {
                MyHomephoneTxt.Text = userInfo.HomePhone ?? "";
                MyMobileTxt.Text = userInfo.Mobile ?? "";
                MyEmailTxt.Text = userInfo.Email ?? "";
            }
            if (deliveryInfo != null)
            {
                BillAddressTxt.Text = deliveryInfo.Address ?? "";
                BillCityTxt.Text = deliveryInfo.CityName ?? "";
                BillCountryDDL.SelectedValue = deliveryInfo.CountryID.ToString() ?? "";
                BillFirstNameTxt.Text = deliveryInfo.FirstName ?? "";
                BillLastNameTxt.Text = deliveryInfo.LastName ?? "";
                BillPostalCodeTxt.Text = deliveryInfo.PostalCode ?? "";
            }
            if (bankInfo != null)
            {
                BankAccountNameTxt.Text = bankInfo.AccountName;
                BankAccountNumberTxt.Text = bankInfo.AccountNumber;
                BankCodeTxt.Text = bankInfo.BankCode;
                //AccountIDTxt.Text = bankInfo.AccountID;
                BankNameTxt.Text = bankInfo.BankName;
                SWIFTCodeTxt.Text = bankInfo.SWIFTCode;
                BICCodeTxt.Text = bankInfo.BICCode;
                IBANCodeTxt.Text = bankInfo.IBANCode;
            }
        }
        protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
        {
            if (e.Item is RadListViewEditableItem && e.Item.IsInEditMode)
            {
                temp = e;
            }
        }

        protected void rcbCity_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            count++;
            if (count == 1)
            {
                a = e.Item.Value;
            }
        }
        protected void rcbCountry_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox combo = temp.Item.FindControl("RabCity") as RadComboBox;
            CityEds.Where = "it.[CountryID] = " + e.Value;
            combo.Items.Clear();
            count = 0;
            combo.DataBind();
        }

        protected void rcbCity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            a = e.Value;
        }

        protected void RadListView1_ItemUpdating(object sender, RadListViewCommandEventArgs e)
        {
            if (GroupUserSettingEds.UpdateParameters.Count > 0)
            {
                GroupUserSettingEds.UpdateParameters.Clear();
            }
            GroupUserSettingEds.UpdateParameters.Add("CityID", DbType.Int64, a);
            a = "";
            count = 0;
        }
    }
}